package com.pheruagor.handmanager.storage.impl.sqlite {
    import com.pheruagor.handmanager.model.business.Club;
    import com.pheruagor.handmanager.utils.TestCaseUtils;

    import flash.events.SQLEvent;

    import flexunit.framework.Assert;
    import flexunit.framework.TestCase;

    import mx.utils.UIDUtil;

    public class ClubSQLiteStorageTest extends TestCase {

        private var m_club:Club;
        private var m_clubSQLiteStorage:ClubSQLiteStorage;

        override public function setUp():void {
            m_club = new Club();
            m_clubSQLiteStorage = new ClubSQLiteStorage();
        }

        //-----------------------------------------------------------
        // SAVE
        //-----------------------------------------------------------
        public function testSaveNullClub():void {
            m_club = null;

            var onResult:Function = function(event:SQLEvent):void {
                };
            var onFault:Function = function(event:SQLEvent):void {
                };

            TestCaseUtils.assertException(function():void {
                    m_clubSQLiteStorage.saveClub(m_club, onResult, onFault);
                }, ArgumentError);
        }

        public function testSaveEmptyClub():void {
            var onResult:Function = function(event:SQLEvent):void {
                };
            var onFault:Function = function(event:SQLEvent):void {
                };

            TestCaseUtils.assertException(function():void {
                    m_clubSQLiteStorage.saveClub(m_club, onResult, onFault);
                }, ArgumentError);
        }

        public function testSaveClubWhithoutId():void {
            m_club.name = "No id club";

            var onResult:Function = function(event:SQLEvent):void {
                };
            var onFault:Function = function(event:SQLEvent):void {
                };

            TestCaseUtils.assertException(function():void {
                    m_clubSQLiteStorage.saveClub(m_club, onResult, onFault);
                }, ArgumentError);
        }

        public function testSaveClubWhithoutName():void {
            m_club.id = UIDUtil.createUID();
            var onResult:Function = function(event:SQLEvent):void {
                };
            var onFault:Function = function(event:SQLEvent):void {
                };

            TestCaseUtils.assertException(function():void {
                    m_clubSQLiteStorage.saveClub(m_club, onResult, onFault);
                }, ArgumentError);
        }

        public function testSaveClubWhithoutDate():void {
            m_club.id = UIDUtil.createUID();
            m_club.name = "My first club";
            m_club.creationDate = new Date(2009, 10, 15, 9);

            var onResult:Function = function(event:SQLEvent):void {
                };
            var onFault:Function = function(event:SQLEvent):void {
                };

            m_clubSQLiteStorage.saveClub(m_club, onResult, onFault);
        }


        public function testSaveSimpleClub():void {
            m_club.id = UIDUtil.createUID();
            m_club.name = "My first club";
            m_club.creationDate = new Date(2009, 10, 15, 9);

            var onResult:Function = function(event:SQLEvent):void {
                    Assert.assertNotNull(event);
                };
            var onFault:Function = function(event:SQLEvent):void {
                };

            m_clubSQLiteStorage.saveClub(m_club, onResult, onFault);
        }

        public function testSaveExistingClub():void {
            var id:String = UIDUtil.createUID();
            m_club.id = id;
            m_club.name = "Club to update";
            m_club.creationDate = new Date(2009, 6, 25, 14);

            var onResult:Function = function(event:SQLEvent):void {
                    Assert.assertNotNull(event);
                };
            var onFault:Function = function(event:SQLEvent):void {
                };

            m_clubSQLiteStorage.saveClub(m_club, onResult, onFault);

            m_club.name = "Updated club";
            m_clubSQLiteStorage.saveClub(m_club, onResult, onFault);
        }

        //-----------------------------------------------------------
        // Load
        //-----------------------------------------------------------
        public function testSimpleListClubs():void {
            var table:Table = new Table();
            table.selectStatement = "* FROM " + table.name + ";";

            var onResult:Function = function(result:Array):void {
                };
            var onFault:Function = function(event:SQLEvent):void {
                };

            m_clubSQLiteStorage.listClubs(onResult, onFault);
        }

        //-----------------------------------------------------------
        // Remove
        //-----------------------------------------------------------


    }
}